//
//  ChartViewController.m
//  MyWallet
//
//  Created by 美鑫科技 on 16/8/9.
//  Copyright © 2016年 MorpLCP. All rights reserved.
//

#import "ChartViewController.h"
#import "ChartTableViewCell.h"

@interface ChartViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, copy) NSString *monthStr;

@end

@implementation ChartViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self initSubViews];
}

- (void)initSubViews
{
    self.title = @"本月份收支图表";
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(
                                                                   0,
                                                                   0,
                                                                   ScreenWidth,
                                                                   ScreenHeight
                                                                   ) style:(UITableViewStylePlain)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerClass:[ChartTableViewCell class] forCellReuseIdentifier:@"chartCell"];
    [self.view addSubview:_tableView];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 5;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ChartTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"chartCell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (indexPath.row == 0)
    {
        cell.title = [NSString stringWithFormat:@"%@月份收支情况走势图  (单位：元)", self.monthStr];
        cell.chartType = 0;
    }
    else if (indexPath.row == 1)
    {
        cell.title = [NSString stringWithFormat:@"%@月份收入途径及金额展示图  (单位：元)", self.monthStr];
        cell.chartType = 1;
    }
    else if (indexPath.row == 2)
    {
        cell.title = [NSString stringWithFormat:@"%@月份支出途径及金额展示图  (单位：元)", self.monthStr];
        cell.chartType = 2;
    }
    else if (indexPath.row == 3)
    {
        cell.title = [NSString stringWithFormat:@"%@月份收入途径比例图  (单位：元)", self.monthStr];
        cell.chartType = 3;
    }
    else if (indexPath.row == 4)
    {
        cell.title = [NSString stringWithFormat:@"%@月份支出途径比例图  (单位：元)", self.monthStr];
        cell.chartType = 4;
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 270.0f;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (NSString *)monthStr
{
    if (!_monthStr)
    {
        if (![[NSUserDefaults standardUserDefaults] objectForKey:@"BudgetDate"])
        {
            _monthStr = [NSString stringWithFormat:@"%d", [[FinalTools currentDateWithFormat:@"MM"] intValue]];
        }
        else
        {
            NSString *dataStr = [[NSUserDefaults standardUserDefaults] objectForKey:@"BudgetDate"];
            if ([[[FinalTools currentDateWithFormat:@"yyyy-MM-dd"] substringWithRange:NSMakeRange(8, 2)] intValue] > [dataStr intValue])
            {
                _monthStr = [NSString stringWithFormat:@"%d", [[FinalTools currentDateWithFormat:@"MM"] intValue]];
            }
            else
            {
                _monthStr = [NSString stringWithFormat:@"%d", [[FinalTools currentDateWithFormat:@"MM"] intValue] - 1];
            }
        }
    }
    return _monthStr;
}

@end
